package com.medical.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.medical.data.domain.HospitalInformation;
import com.medical.data.exception.MyException;
import com.medical.data.mapper.HospitalInformationMapper;
import com.medical.data.service.HospitalInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.data.utils.ResultCode;
import com.medical.data.vo.HospitalVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 医院信息 服务实现类
 * </p>
 *
 * @author cj
 * @since 2024-02-06
 */
@Service
public class HospitalInformationServiceImpl extends ServiceImpl<HospitalInformationMapper, HospitalInformation> implements HospitalInformationService {

    @Override
    public List<HospitalInformation> getHospital() {
        List<HospitalInformation> hospitalInformations = baseMapper.selectList(null);
        return hospitalInformations;
    }

    @Override
    public void insertHospital(HospitalVo hospitalVo) {
        //首先查询该医院是否存在
        String hsName = hospitalVo.getHsName();
        HospitalInformation hospitalInformation = baseMapper.selectOne(Wrappers.<HospitalInformation>lambdaQuery().eq(HospitalInformation::getHsName, hsName));
        if (!ObjectUtils.isEmpty(hospitalInformation)){
            throw new MyException(ResultCode.ERROR.getCode(), "该医院已存在");
        }
        HospitalInformation hospitalInformation1 = new HospitalInformation().setHsName(hospitalVo.getHsName())
                        .setHsDesc(hospitalVo.getHsDesc())
                                .setHsLevel(hospitalVo.getHsLevel());
        int insert = baseMapper.insert(hospitalInformation1);
        if (insert <1){
            throw new MyException(ResultCode.ERROR.getCode(), "新增医院失败");
        }
    }

    @Override
    public void isExistHospital(String hospitalId) {
        LambdaQueryWrapper<HospitalInformation>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HospitalInformation::getId,hospitalId).select(HospitalInformation::getId);
        HospitalInformation hospitalInformation = this.baseMapper.selectOne(wrapper);
        if(ObjectUtils.isEmpty(hospitalInformation)){
            throw new MyException(ResultCode.ERROR.getCode(), "医院id错误");
        }
    }

    public String getNameById(String hospitalId){
        LambdaQueryWrapper<HospitalInformation>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HospitalInformation::getId,hospitalId);
        return this.baseMapper.selectOne(wrapper).getHsName();
    }

}
